
const express = require('express')
const mongoose =require('mongoose')
//链接mongoose
const db_url = 'mongodb://localhost:27017/reactApp'
mongoose.connect(db_url)
mongoose.connection.on('connected',function(){
    console.log('mongo linked')
})
//新建表
const Goods = mongoose.model('goods',new mongoose.Schema({
    'wine': {type:String,require:true},
    'num': {type:Number,require:true}
}))

//创建新的数据
// Goods.create({
//     wine:'洋河大曲',
//     num: 500
// },function(err,doc){
//     if(!err) {
//         console.log(doc)
//     }else{
//         console.log(err)
//     }
// })
//删除数据
Goods.remove({num:100},function(err,doc){
    if(!err){
        console.log(doc)
    }
})
//更新数据
    // Goods.update({wine:'wuliangye'},{'$set':{num:365}},function(err,doc){
    //     console.log(doc)
    //     console.log('更改成功')
    // })


const app = express()

app.get('/',function(req,res){
    res.send('<h1>hello word</h1>')
})
app.get('/data',function(req,res){
    res.json({name:'zoiew'})
})
app.get('/goodslist',function(req,res){
    // res.json({name:'jack',age:'29'})
    // Goods.find({wine:'wuliangye'},function(err,doc){
    //     res.json(doc)
    // })
    Goods.findOne({wine:'wuliangye'},function(err,doc){
        res.json(doc)
    })

})

app.get('/goods',function(req,res){
    Goods.find({},function(err,doc){
        res.json(doc)
    })
})

app.listen(9093,function(){
    console.log('node app start by zoiew')
})